﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SharpDX;
using SharpDX.DXGI;
using SharpDX.Direct3D11;
using SharpDX.Direct3D;
using SharpDX.D3DCompiler;

namespace SharpDX
{
    public class CModel
    {
        public SharpDX.Direct3D11.Buffer vertexBuffer;
        public SharpDX.Direct3D11.Buffer indexBuffer;
        public int vertexCount;
        public int indexCount;

        public CTexture texture;

        public CModel()
        {

        }
        public bool Initialize(SharpDX.Direct3D11.Device device,string filename)
        {
            if (!InitializeBuffer(device))
                return false;
            if (!LoadTexture(device, filename))
                return false;
            return true;
        }
        private bool LoadTexture(SharpDX.Direct3D11.Device device,string filename)
        {
            texture = new CTexture();
            texture.Initialize(device, filename);
            return true;
        }
        private bool InitializeBuffer(SharpDX.Direct3D11.Device device)
        {
            vertexCount = 4;
            indexCount = 4;
            var vertices = new[]
            {
                new CTextureShader.Vertex()
                {
                    position=new Vector3(-1,-1,0),
                    texture=new Vector2(0,0)
                },
                new CTextureShader.Vertex()
                {
                    position=new Vector3(-1,1,0),
                    texture=new Vector2(0,1)
                },
                new CTextureShader.Vertex()
                {
                    position=new Vector3(1,-1,0),
                    texture=new Vector2(1,0)
                },
                new CTextureShader.Vertex()
                {
                    position=new Vector3(1,1,0),
                    texture=new Vector2(1,1)
                }
            
            };

            var indices = new[]
            {
                0,1,2,3
            };

            vertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices);
            indexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indices);
            return true;
        }
        public void Shutdown()
        {
            // Release the model texture.
            ReleaseTexture();

            // Release the vertex and index buffers.
            ShutdownBuffers();
        }
        private void ReleaseTexture()
        {
            // Release the texture object.
            if (texture != null)
            {
                texture.ShutDown();
                texture = null;
            }
        }
        public void Render(DeviceContext deviceContext)
        {
            RenderBuffer(deviceContext);
        }
        private void RenderBuffer(DeviceContext deviceContext)
        {
            deviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, Utilities.SizeOf<CTextureShader.Vertex>(), 0));
            deviceContext.InputAssembler.SetIndexBuffer(indexBuffer, Format.R32_UInt, 0);
            deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip;
        }
        private void ShutdownBuffers()
        {
            // Return the index buffer.
            if (indexBuffer != null)
            {
                indexBuffer.Dispose();
                indexBuffer = null;
            }

            // Release the vertex buffer.
            if (vertexBuffer != null)
            {
                vertexBuffer.Dispose();
                vertexBuffer = null;
            }
        }
    }
}
